ggrent_plot <- function(
  dat,
  x,
  y,
  group,
  weight = NA_real_,
  num_splines = 1L,
  heights = c(4, 2),
  xlim = rep(NA_real_, 2L),
  ylim = rep(NA_real_, 2L),
  font_size = 15L
) {
  ## Prediction plot
  prediction <- ggplot() +
    geom_smooth(
      data = dat,
      aes(
        x = !!as.name(x),
        y = !!as.name(y),
        weight = !!as.name(weight),
        group = !!as.name(group),
        fill = !!as.name(group),
        color = !!as.name(group)
      ),
      formula = y ~ ns(x, num_splines),
      method = "glm",
      method.args = list(family = 'binomial')
    ) +
    scale_colour_viridis_d(begin = 0, end = 0.5) + 
    scale_fill_viridis_d(begin = 0, end = 0.5) +
    facet_grid(~ get(eval(group))) +
    theme(legend.position = "none") +
    xlab(NULL) + 
    coord_cartesian(xlim =xlim) + 
    coord_cartesian(ylim = ylim) +
    theme(text = element_text(size = font_size))
  
  ## Density plot
  density <- ggplot() +
    geom_density(
      data = dat %>%
        filter(!!as.name(y) == 0),
      aes(
        y = ..count../ nrow(dat),
        x = !!as.name(x),
        weight = !!as.name(weight),
        group = !!as.name(group),
        fill = !!as.name(group),
        color = !!as.name(group)
      ),
      alpha = .2
    ) +
    geom_density(
      data = dat %>%
        filter(!!as.name(y) == 1),
      aes(
        y = ..count../ nrow(dat),
        x = !!as.name(x),
        weight = !!as.name(weight),
        group = !!as.name(group),
        fill = !!as.name(group),
        color = !!as.name(group)
      ),
      alpha = .4
    ) +
    coord_cartesian(xlim =xlim) + 
    ylab("Density") + 
    scale_colour_viridis_d(begin = 0, end = 0.5) + 
    scale_fill_viridis_d(begin = 0, end = 0.5) +
    facet_grid(~ get(eval(group))) +
    theme(legend.position = "none",
          strip.background = element_blank(),
          strip.text.x = element_blank()) +
    theme(text = element_text(size = font_size))
  
  comb_plot <- grid.arrange(
    prediction,
    density,
    nrow = 2,
    heights = heights
  )
  
  return(comb_plot)
}
